<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class News_model extends CI_Model
{
    public function __construct() 
    { 
        parent::__construct(); 
    }
    
    public function get_all_news($nr=10,$published=1,$offset='')
    {
        if(!empty($offset))
            $wynik = $this->db->order_by('news_date','DESC')->get('view_news',$nr,$offset);
        else
            $wynik = $this->db->order_by('news_date','DESC')->get('view_news',$nr);
        
        if($wynik->num_rows()>0)
        {
            $wynik = $wynik->result_array();
            $tab['stan'] = 1;
            $tab['news'] = array();
            foreach($wynik as $w)
            {
                $tab['news'][$w['news_id']]['title']    = $w['news_title'];
                $tab['news'][$w['news_id']]['slug']     = $w['news_slug'];
                $tab['news'][$w['news_id']]['intro']    = $w['news_intro'];
                $tab['news'][$w['news_id']]['text']     = $w['news_text'];
                $tab['news'][$w['news_id']]['autor']    = $w['user_fname'].' '.$w['user_lname'];
                $tab['news'][$w['news_id']]['date']     = $w['news_date'];
                $tab['news'][$w['news_id']]['update']   = $w['news_updated'];
                $tab['news'][$w['news_id']]['image']    = $w['news_image'];
                $tab['news'][$w['news_id']]['cat']['id']    = $w['cat_id'];
                $tab['news'][$w['news_id']]['cat']['name']  = $w['cat_name'];
                $tab['news'][$w['news_id']]['tags'][$w['tag_id']] = $w['tag_name'];
            }
        }
        else
            $tab['stan'] = 0;
        return $tab;
    }
    
    public function insert_news($news,$newscat)
    {
        $this->db->insert('ut_news',$news);
        if($this->db->affected_rows()>0)
        {
            $newscat['nc_news'] = $this->db->insert_id();
            $this->db->insert('ut_newscat',$newscat);
            if($this->db->affected_rows()>0)
                $newscat['stan'] = TRUE;                
            else
                $newscat['stan'] = FALSE;
        }
        else
            $newscat['stan'] = FALSE;
        return $newscat;
    }
    
    public function insert_category($tab)
    {
        $this->db->insert('ut_categories',$tab);
        if($this->db->affected_rows()>0)
            return TRUE;
        else
            return FALSE;
    }
    
    public function get_categories($subs=TRUE)
    {
        $list = '';
        $wynik = $this->db
                ->where('cat_parent IS NULL',null)
                ->or_where('cat_parent =',0)
                ->get('ut_categories');
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = 1;            
            $rodzic = $wynik->result_array();
            foreach($rodzic as $p)
            {
                $tab['cats'][$p['cat_id']]['name'] = $p['cat_name'];
                if($subs)
                {
                    $q = $this->db->get_where('ut_categories',array('cat_parent'=>$p['cat_id']));
                    if($q->num_rows()>0)
                    {
                        foreach($q->result_array() as $w)
                            $tab['cats'][$p['cat_id']]['subs'][$w['cat_id']]['name'] = $w['cat_name'];
                    }
                }
            }
        }
        else
            $tab['stan'] = 0;
        return $tab;
    }
    
    public function update_order($tab)
    {
        array_shift($tab);
        $update = array();
        foreach($tab as $t)
        {
            if($t->parent_id=='root')
                $pid = 0;
            else
                $pid = $t->parent_id;
            $update[] = array(
                'cat_id'    => $t->item_id,
                'cat_parent'=> $pid
            );
        }
        $this->db->update_batch('ut_categories',$update,'cat_id');
        if($this->db->affected_rows()>0)
            return TRUE;
        else
            return FALSE;
    }

    protected function category_tree($parent)
    {
        if($this->has_child($parent))
        {
            $q      = $this->db->get_where('ut_categories',array('cat_parent'=>$parent));
            $wynik  = $this->db->query($q);
            $sub    = $wynik->result_array();
            foreach($sub as $s)
            {
                $tab['subs'][$s['cat_id']]['name'] = $s['cat_name'];
                $tab['subs'][$s['cat_id']] = $this->category_tree($s['cat_id']);
            }
        }
        else
            $tab['subs'] = '';
        return $tab;
    }
    
    protected function has_child($id)
    {
        $wynik = $this->db->get_where('ut_categories',array('cat_parent'=>$id));
        if($wynik->num_rows()>0)
            return TRUE;
        else
            return FALSE;
    }
    
    public function get_tags()
    {
        $wynik = $this->db->get('ut_tags');
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = 1;
            $tab['tags'] = array();
            $wynik = $wynik->result_array();
            foreach($wynik as $w)
                $tab['tags'][$w['tag_id']] = $w['tag_name']; 
        }
        else
            $tab['stan'] = 0;
        return $tab;
    }
    
    public function get_news($id)
    {
        $wynik = $this->db->get_where('view_news',array('news_id'=>$id));
        if($wynik->num_rows()>0)
        {
            $tab['stan'] = TRUE;
            foreach($wynik->result_array() as $n)
            {
                $tab['wynik']['nid']    = $n['news_id'];
                $tab['wynik']['title']  = $n['news_title'];
                $tab['wynik']['intro']  = $n['news_intro'];
                $tab['wynik']['text']   = $n['news_text'];
                $tab['wynik']['image']  = $n['news_image'];
                $tab['wynik']['slug']   = $n['news_slug'];
                $tab['wynik']['date']   = $n['news_date'];
                $tab['wynik']['update'] = $n['news_updated'];
                $tab['wynik']['seo']['title'] = $n['news_seotitle'];
                $tab['wynik']['seo']['desc']  = $n['news_seodesc'];
                $tab['wynik']['seo']['keys']  = $n['news_seokeys'];
                $tab['wynik']['author'] = $n['user_fname'].' '.$n['user_lname'];
                $tab['wynik']['uid']    = $n['user_id'];
                $tab['wynik']['cat'][$n['cat_id']] = $n['cat_name'];
                $tab['wynik']['tag'][$n['tag_id']] = $n['tag_name'];
            }
        }
        else
            $tab['stan'] = FALSE;
        return $tab;
    }
}